from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash

db = SQLAlchemy()


class User(db.Model, UserMixin):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    phone_number = db.Column(db.String(11), nullable=True)
    gender = db.Column(db.String(2), default='男')
    age = db.Column(db.Integer, default=25)
    height = db.Column(db.Float, default=175.0)
    weight = db.Column(db.Float, default=70.0)
    goal = db.Column(db.String(20), default='保持健康')
    avatar = db.Column(db.String(255), default='images/avatar-placeholder.png')
    register_time = db.Column(db.DateTime, default=datetime.now)
    is_admin = db.Column(db.Boolean, default=False)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def get_id(self):
        return str(self.id)

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'email': self.email,
            'phone_number': self.phone_number,
            'phoneNumber': self.phone_number,
            'gender': self.gender,
            'age': self.age,
            'height': self.height,
            'weight': self.weight,
            'goal': self.goal,
            'userType': self.user_type if hasattr(self, 'user_type') else '普通用户',
            'avatar': self.avatar,
            'registerTime': self.register_time.strftime('%Y-%m-%d %H:%M:%S') if self.register_time else ''
        }


class HealthKnowledge(db.Model):
    __tablename__ = 'health_knowledge'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text, nullable=False)
    category = db.Column(db.String(50), nullable=False)
    author = db.Column(db.String(50), nullable=True)
    view_count = db.Column(db.Integer, default=0)
    image_url = db.Column(db.String(255), nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(
        db.DateTime, default=datetime.now, onupdate=datetime.now)


class Post(db.Model):
    __tablename__ = 'posts'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey(
        'users.id'), nullable=False)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text, nullable=False)
    view_count = db.Column(db.Integer, default=0)
    like_count = db.Column(db.Integer, default=0)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(
        db.DateTime, default=datetime.now, onupdate=datetime.now)

    # 关联关系
    user = db.relationship('User', backref=db.backref('posts', lazy=True))
    comments = db.relationship(
        'Comment', backref='post', lazy=True, cascade='all, delete-orphan')


class Comment(db.Model):
    __tablename__ = 'comments'

    id = db.Column(db.Integer, primary_key=True)
    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.now)

    # 关联用户
    user = db.relationship('User', backref=db.backref('comments', lazy=True))
